package com.qinggeng.framework.config.mq.impl;

import com.alibaba.fastjson.JSON;
import com.qinggeng.common.enums.AppTurnPageEnum;
import com.qinggeng.common.enums.MessageTypeEnum;
import com.qinggeng.common.enums.OrdersStatusCodeEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.framework.config.mq.dto.MessageGoodsRefundMessageDTO;
import com.qinggeng.framework.config.mq.dto.MessageOrderSingleltemDTO;
import com.qinggeng.framework.config.mq.handle.MessageHandle;
import com.qinggeng.project.system.domain.*;
import com.qinggeng.project.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class GoodsRefundMessageJPushHandleImpl implements MessageHandle {
    @Autowired
    private IUserAliasService userAliasService;
    @Autowired
    private IUserMessageService messageService;
    @Autowired
    private IOrdersGoodsService ordersGoodsService;
    @Autowired
    private UserAccountRecordOrderSingleItemService singleItemService;
    @Autowired
    private MessagePushService messagePushService;

    @Override
    public void handle(String msg) {
        log.info("商品退款消息推送处理"+msg);
        MessageGoodsRefundMessageDTO refundMessageDTO= JSON.parseObject(msg,MessageGoodsRefundMessageDTO.class);
        UserAccountRecordOrderSingleItem orderSingleItem=singleItemService.getUserAccountRecordOrderSingleItem(refundMessageDTO.getOrdersGoodsId());
        if(null==orderSingleItem){
            throw new CustomException("申请退款记录不存在");
        }
        try{
            //发送站内信息
            String message=insertUserMessage(orderSingleItem,refundMessageDTO.isSuccess());
            //根据用户ID查询普通用户的别名
            UserAlias userAlias=userAliasService.selectMemberUserAliasByUserId(orderSingleItem.getUserId());
            if(null==userAlias){
                return;
            }
            //极光推送消息
            JPushExtraModel extra=new JPushExtraModel(AppTurnPageEnum.GOODS_REFUND_INFO,refundMessageDTO.getOrdersGoodsId().toString());
            messagePushService.JPushMessage(userAlias,message,extra);

        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

    /**
     * 发送站内信息
     * @param item
     * @param success
     * @return
     */
    private String insertUserMessage(UserAccountRecordOrderSingleItem item,boolean success){

        //查询退款的订单商品
        OrdersGoods ordersGoods=ordersGoodsService.selectOrdersGoodsById(item.getOrderGoodsId());
        if(null==ordersGoods){
            throw new CustomException("要退款的订单商品不存在");
        }

        //保存消息记录到数据库
        UserMessage userMessage = new UserMessage();
        userMessage.setUserId(item.getUserId());
        userMessage.setMessageType(MessageTypeEnum.退款消息.getValue());
        userMessage.setFkId(item.getOrderGoodsId());
        userMessage.setFkMark("商品退款通知");
        String message;
        if(success){
            message="尊敬的用户，您的商品"+ordersGoods.getGoodsName()+"已退款成功！";
            userMessage.setStatusCode(OrdersStatusCodeEnum.已退单.getCode());
        }else{
            message="尊敬的用户，您的商品"+ordersGoods.getGoodsName()+"退款已被拒绝！";
            userMessage.setStatusCode(OrdersStatusCodeEnum.退单失败.getCode());
        }
        userMessage.setFkContent(message);
        userMessage.setCreateTime(DateUtils.getNowDate());
        int rows=messageService.insertUserMessage(userMessage);
        if(rows<1){
            log.info("发送站内信息失败！");
        }
        return message;
    }
}
